import SwiftUI

struct StepperView: View {
    // MARK: - 公开属性
    @Binding var value: Double
    var minValue: Double = 1
    var maxValue: Double = 8
    var step: Double = 1
    var buttonColor: Color = Color.backgroundColor()
    var textColor: Color = .primary
    var cornerRadius: CGFloat = 8
    var buttonSize: CGFloat = 32
    
    // MARK: - 视图体
    var body: some View {
        HStack(spacing: 10) {
            // 减号按钮
            Button(action: decrementValue) {
                Text("-")
                    .font(.system(size: 24, weight: .bold))
                    .foregroundColor(Color.red)
                    .frame(width: buttonSize, height: buttonSize)
                    .background(isMinValue ? buttonColor.opacity(0.5) : buttonColor)
                    .cornerRadius(cornerRadius)
            }
            .disabled(isMinValue)
            
            // 数值显示
            Text("\(Int(value)) Km")
                .font(.system(size: 18, weight: .medium))
                .foregroundColor(textColor)
                .frame(minWidth: buttonSize)
                .padding(.horizontal, 8)
            
            // 加号按钮
            Button(action: incrementValue) {
                Text("+")
                    .font(.system(size: 24, weight: .bold))
                    .foregroundColor(Color.red)
                    .frame(width: buttonSize, height: buttonSize)
                    .background(isMaxValue ? buttonColor.opacity(0.5) : buttonColor)
                    .cornerRadius(cornerRadius)
            }
            .disabled(isMaxValue)
        }
    }
    
    // MARK: - 私有计算属性
    private var isMinValue: Bool {
        value <= minValue
    }
    
    private var isMaxValue: Bool {
        value >= maxValue
    }
    
    // MARK: - 动作方法
    private func decrementValue() {
        if value > minValue {
            value -= step
        }
    }
    
    private func incrementValue() {
        if value < maxValue {
            value += step
        }
    }
}

//// MARK: - 预览
//struct StepperView_Previews: PreviewProvider {
//    static var previews: some View {
//        StepperView(value:3)
//    }
//}    
